package com.srzk.youthleagueback.service;

import com.srzk.youthleagueback.entity.po.Class;
import com.srzk.youthleagueback.entity.po.News;
import com.srzk.youthleagueback.mapper.ClassMapper;
import com.srzk.youthleagueback.mapper.NewsMapper;
import com.srzk.youthleagueback.results.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.srzk.youthleagueback.results.CodeMsg.*;

@Service
public class NewsService {
    @Autowired
    NewsMapper newsMapper;

    public Result<News> add(News news) {
        int result = newsMapper.insert(news);
        if (result == 1) {
            return Result.success(news);
        } else {
            return Result.error(ADD_FAIL);
        }
    }

    public Result<List<News>> getList() {
        List<News> classes = newsMapper.selectAll();
        if (classes == null || classes.isEmpty()) {
            return Result.error(NOT_EXIST);
        } else {
            return Result.success(classes);
        }
    }


    public Result<List<News>> getByTeacherAndCourse(String course,String teacher) {
        List<News> news = newsMapper.selectByTeacherAndCourse(teacher,course);
        if (news == null||news.isEmpty()) {
            return Result.error(NOT_EXIST);
        } else {
            return Result.success(news);
        }
    }

    public Result<News> update(News news) {
        int result = newsMapper.updateByPrimaryKey(news);
        if (result == 1) {
            return Result.success(news);
        } else {
            return Result.error(UPDATE_FAIL);
        }
    }

}
